package com.liuhuan.sendmessage.controller;

import com.cloopen.rest.sdk.BodyType;
import com.cloopen.rest.sdk.CCPRestSmsSDK;
import com.liuhuan.sendmessage.service.SmsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Set;

@RestController
@Api(tags = "验证码管理")

public class SMSController {
    @Autowired
    private SmsService smsService;

    @RequestMapping("/send")
    @ApiOperation("发送验证码")
    public String send(@RequestParam String to) {
        String templateId = "1";
        String[] datas = {"6668", "3", "1"};
        String subAppend = "1234";  // 可选 扩展码，四位数字 0~9999
        HashMap<String, Object> result = smsService.sendSms(to, templateId, datas, subAppend);
        String code= (String) result.get("statusCode");
        if (code.equals(result.get("statusCode"))) {
            // 正常返回输出data包体信息（map）
            HashMap<String, Object> data = (HashMap<String, Object>) result.get("data");
            Set<String> keySet = data.keySet();
            for (String key : keySet) {
                Object object = data.get(key);
                System.out.println(key + " = " + object);
            }
            return "发送成功";

        } else {
            // 异常返回输出错误码和错误信息
            System.out.println("错误码=" + result.get("statusCode") + " 错误信息= " + result.get("statusMsg"));
            return "发送失败";
        }
    }
}
